package order.dao;

import order.pojo.food_information;
import order.pojo.manager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class foodDao {


    /**
     * @JackSu 菜品的增删改查，遍历，根据id查找
     * food_img这个字段用来存图片名称
     */

//querryAll
    public List<food_information> querrAllFood(Connection con) throws SQLException {
        List<food_information> list = new ArrayList<food_information>();
        String sql = "select * from food_information order by food_id";
        PreparedStatement pstm = con.prepareStatement(sql);
        ResultSet rs = pstm.executeQuery();

        food_information food = null;
        while (rs.next()) {
            food = new food_information();
            food.setFood_id(rs.getInt("food_id"));
            food.setFood_name(rs.getString("food_name"));
            food.setFood_price(rs.getFloat("food_price"));
            food.setFood_number(rs.getString("food_number"));
            food.setStore_id(rs.getInt("store_id"));
            food.setFood_img(rs.getString("food_img"));
            food.setFood_publisher(rs.getString("food_publisher"));
            list.add(food);
            System.out.println(list.toString());
        }
        return list;

    }
    //查询出已经发布的菜品
    public List<food_information> selectFoodByPublisher(Connection con) throws SQLException {
        List<food_information> list = new ArrayList<food_information>();
        String sql = "select * from food_information where food_publisher='1' order by food_id";
        PreparedStatement pstm = con.prepareStatement(sql);
        ResultSet rs = pstm.executeQuery();

        food_information food = null;
        while (rs.next()) {
            food = new food_information();
            food.setFood_id(rs.getInt("food_id"));
            food.setFood_name(rs.getString("food_name"));
            food.setFood_price(rs.getFloat("food_price"));
            food.setFood_number(rs.getString("food_number"));
            food.setStore_id(rs.getInt("store_id"));
            food.setFood_img(rs.getString("food_img"));
            food.setFood_publisher(rs.getString("food_publisher"));
            list.add(food);
            System.out.println(list.toString());
        }
        return list;

    }


    //根据id删除一个
    public int deleteFoodById(Connection con, Integer foodId) throws SQLException {
        String sql = "delete from food_information where food_id=?";
        PreparedStatement pstm = con.prepareStatement(sql);
        pstm.setInt(1, foodId);
        return pstm.executeUpdate();

    }

    //修改发布状态
    public int updateFoodById(Connection con, String food_publisher, Integer foodId) throws SQLException {
        String sql = "update food_information set food_publisher=?where food_id=?";
        PreparedStatement pstm = con.prepareStatement(sql);
        pstm.setString(1, food_publisher);
        pstm.setInt(2, foodId);

        return pstm.executeUpdate();
    }

    //增加一个菜品信息
    public int addFood(Connection con, food_information food) throws SQLException {
        String sql = "insert into food_information values(?,?,?,?,?,?,?)";
        PreparedStatement pstm = con.prepareStatement(sql);
        pstm.setInt(1, food.getFood_id());
        pstm.setString(2, food.getFood_name());
        pstm.setFloat(3, food.getFood_price());
        pstm.setString(4, food.getFood_number());
        pstm.setInt(5, food.getStore_id());
        pstm.setString(6, food.getFood_img());
        pstm.setString(7, food.getFood_publisher());

        return pstm.executeUpdate();

    }
}



